<template>
  <view class="legalPersonChoosesUsers">
    <head-box></head-box>
    <view class="tab-botton activityReservation-page">
      <view class="_main-content">
        <view class="textarea_box">
                <textarea :disabled="disabled" maxlength="3000" adjust-position="true"
                          @input="changgevalue" v-model="rylist"
                          placeholder="此处粘贴一整段信息，自动识别姓名电话和身份证。例如：张三,135****8888,3301**********3019 友情提示：中间用逗号分割，多个信息换行分开" />
        </view>
        <view class="uni-form-item uni-column">
          <!--                <uni-section title="报名信息" type="line"></uni-section>
  -->
          <view class="example-body" style="">
            <view class="uni-flex uni-row">
              <view class="btn" @click="addSku">添加企业成员</view>
            </view>
            <view class="uni-form-item uni-column">
              <view class="ks"  v-for="(tmp,index) in skuArray" :key="index"
                    :data-id="index"
                    :style="{border:tmp.type?'1rpx solid red':''}">
                <view class="_main-item">
                  <view class="_main-item-chunk _main-item-label"><text>姓名</text><text
                      class="xing">*</text></view>
                  <view class="_main-item-chunk _main-item-val">
                    <input cursorSpacing="10" class="_main-item-val-input" type="text"
                           maxlength="15" v-model="tmp.name" placeholder="请输入姓名" />
                  </view>
                  <view class="text" style="width: 100rpx;">
                    <image class=""
                           style="position: relative;width: 60rpx;height: 60rpx;top:10%;left:20rpx;"
                           src="../../static/img/del.png" :data-src="index" @tap="delSku">
                    </image>
                  </view>
                </view>
                <view class="_main-item">
                  <view class="_main-item-chunk _main-item-label"><text>身份证</text> </view>
                  <view class="_main-item-chunk _main-item-val">
                    <input cursorSpacing="10" class="_main-item-val-input" type="text"
                           v-model="tmp.sex" placeholder="请输入身份证" />
                  </view>
                </view>
                <view class="_main-item">
                  <view class="_main-item-chunk _main-item-label"><text>电话</text><text
                      class="xing">*</text></view>
                  <view class="_main-item-chunk _main-item-val">
                    <input cursorSpacing="10" class="_main-item-val-input"
                           :data-tel="tmp.tel" type="text" maxlength="11" v-model="tmp.tel"
                           placeholder="请输入联系电话" />
                  </view>
                </view>
              </view>

            </view>
          </view>
        </view>

        <view class="_main-content-confirm" @tap="addCompany"><text>提交</text></view>
      </view>

    </view>
    <tabs-box></tabs-box>
  </view>
</template>

<script>
import { batchaddUsercommember} from '@/api/usercommember'
import {
  identityCodeValid,
  checkMobile
} from '@/common/validateIdent.js'
export default {
  data() {
    return {
      disabled:false,//是否要禁用
      rylist: '',//编辑框内容
      skuArray: [],//所选择的数据
    };
  },
  methods: {
    changgevalue () {
      this.skuArray = []
      const val = this.rylist.replace ( /\r/g , '' );
      const list = val.split ( '\n' );
      list.forEach ( ( item , index ) => {
        const women = item.split ( ',' );
        if ( women.length >= 1 ) {
          const val = this.skuArray.some ( x => x.tel == women[1] )
          if ( val ) {
            this.skuArray.push ( {
              id : index ,
              name : women[0] ,
              tel : women[1] ,
              sex : women[2] ,
              type : true
            } );
          } else {
            if ( women.length == 2 ) {
              this.skuArray.push ( {
                id : index ,
                name : women[0] ,
                tel : women[1] ,
                sex : '' ,
                type : false
              } );
            } else if ( women.length == 3 ) {
              this.skuArray.push ( {
                id : index ,
                name : women[0] ,
                tel : women[1] ,
                sex : women[2] ,
                type : false
              } );
            }
          }
        }
      } )
    } ,
    addCompany() {
      const comUserList = []
      for (let i = 0; i < this.skuArray.length; i++) {
        const ele = this.skuArray[i];
        if (!ele.name) {
          return this.$api.msg('请输入姓名');
        }
        ele.sex = ele.sex.toUpperCase() //转换成大写
        if (ele.sex != '') {
          const isIdCard = identityCodeValid(ele.sex);
          if (!isIdCard) {
            return this.$api.msg(ele.name + '请输入正确的身份证');
          }
        }
        if (!checkMobile(ele.tel)) {
          return this.$api.msg('请输入正确的手机号');
        }
        if (!!ele.name && !!ele.tel) {
          comUserList.push({
            'userPhone': ele.tel,
            'userPname': ele.name,
            'userPid': ele.sex
          })
        }
      }
      if(!comUserList.length){
        this.$api.msg('至少得添加一条吧')
        return
      }
      if (this.isListSome()) {
        this.$api.msg('已出现重复，请先进行去重处理')
        return
      }
      const data = {
        'gpUsercommemberList': comUserList
      }
      this.$api.showLoading({title:'添加成员中...'})
      batchaddUsercommember(data).then(res=>{
        const{code,message,result} = res
        this.$api.hideLoading()
        if(code===200){
          this.$api.msg(result)
          this.$api.handleMenu('/pages/corporateManagement/corporateManagement')
        } else {
          this.$api.msg(message)
        }
      }).catch(e=>{
        this.$api.hideLoading()
      })
    },
    //验证是不是有重复的数据
    isListSome() {
      let bool = false
      const val = this.skuArray.some((x,i)=>{
        return x.type
      })
      if (val) {
        bool = true
      }
      return bool
    },
    //判断是否有相同的数据
    isRepeat(arr,key){
      const obj = {};

      for(let i=0;i<arr.length;i++){
        if(obj[arr[i][key]]){
          return false;
        }else{
          obj[arr[i][key]] = arr[i];
        }
      }
      return obj;
    },
    //添加列表
    addSku(e) {
      let types = true
      this.skuArray.map((item,index)=>{

        if(index>-1){
          if(!item.name.length){
            this.$api.msg(`请先添加第${index+1}项的必要姓名`)
            types = false
            return false
          }  if(!item.tel.length){
            types = false
            return this.$api.msg(`请输入第${index+1}项的手机号码`);
          }
          if (!checkMobile(item.tel)) {
            types = false
            return this.$api.msg('请输入正确的手机号');
          }
        }

      })
      const type = this.isRepeat( this.skuArray,'tel')
      if(!type){
        this.$api.msg('已存在重复的手机号码')
        this.skuArray.map(item=>item.type=true)
        return
      }
      if(types){
        this.skuArray.push({
          name: '',
          sex: '',
          tel: '',
          type: false,
        })
        this.skuArray.map(item=>item.type=false)
      }


    },
    //删除某一行或者列表
    delSku(e) {
      this.skuArray.splice(e.target.dataset.src, 1);

      if (this.rylist !== '') {
        const val = this.rylist.replace(/\r/g, '');
        const obj = {}

        const list = val.split('\n');
        const arr = this.skuArray.filter((item, s1) => {
          const arrIndex = `${item.name},${item.tel},${item.sex}`
          if (list[s1]) {
            if (list[s1].split(',')[1] === item.tel) {
              obj['str'] = arrIndex
            }
            return obj
          }
        })
        let str = ''
        arr.filter((item, index) => {
          str += `${item.name},${item.tel},${item.sex}\n`
          return str
        })

        this.rylist = str
        this.changgevalue()
      }
    },
  }
}
</script>

<style lang="scss" scoped>
@import '@/style/global-textare-box'
</style>
<style lang="scss" scoped>
._main-content {
  width: 100%;
  box-sizing: border-box;
  padding: 10rpx 40rpx;
}
.activityReservation-page {
  width: 100%;
  box-sizing: border-box;

  .top-fixed {
    position: fixed;
    top: 0;
    z-index: 1;
  }

  .state {
    label {
      margin: 0 10rpx;
    }
  }

  .top {
    width: 100%;
    height: 100rpx;
    padding: 0 30rpx;
    // background-color: #007AFF;
    background-color: #f7f7f7;
    box-shadow: 0 1px 6rpx #ccc;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;

    .top-text {
      color: #000;
      font-size: 34rpx;
    }
  }


}
.ks {
  padding: 30rpx;
  background-color: #f8f8f8;
  margin: 20rpx 0;
  border-radius: 20rpx;
}

.top-box {
  width: 100%;
  height: 100%;
  position: relative;
  text-align: center;
  line-height: 100rpx;

  .back-img {
    width: 44rpx;
    height: 40rpx;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    margin-left: 0rpx;
  }
}

.activityReservation-page-main {
  position: relative;
  width: 90%;
  margin: 0 auto;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 5px 10px rgba(0,0,0,.2);
  background: #fff;

  ._main-tab-box {
    width: 100%;
    height: 80rpx;
    padding-top: 10rpx;
    margin-bottom: 20rpx;
    background-color: #fff;
    display: flex;
    flex-wrap: nowrap;
    justify-content: flex-start;
    // position: fixed;
    // top: 100rpx;
    z-index: 1;

    ._tab-btn {
      width: 15%;
      height: 100%;
      margin-left: 10px;
      line-height: 80rpx;
      color: #777;
      font-size: 28rpx;
      text-align: center;
    }

    ._tab-btn-active {
      color: #000;

      ._btn-text {
        border-bottom: 4rpx solid #037bff;
        padding: 0 0 15rpx;
        font-size: 30rpx;
        box-sizing: border-box;
      }

    }
  }

  ._main-content {
    width: 100%;
    box-sizing: border-box;
    padding: 10rpx 40rpx;

    ._main-content-win {
      width: 100%;
      overflow-x: hidden;

      ._main-content-box {
        //width: 200%;
        display: flex;
        flex-wrap: nowrap;
        justify-content: flex-start;
        transition: all 0.2s linear;
        margin-top: 20rpx;

        ._main-content-box-item {
          width: 100%;

          ._main-item {
            // height: 86rpx;
            border: 1px solid rgba(200, 199, 204, 0.5);
            padding: 5rpx 20rpx;
            border-radius: 10rpx;
            margin-bottom: 40rpx;
            display: flex;
            flex-wrap: nowrap;
            justify-content: space-between;
            align-items: center;

            ._main-item-chunk {
              font-size: 34rpx;
              color: #000;
              line-height: 84rpx;
            }

            ._main-item-label {
              //width: 35%;
              //min-width: 210rpx;
              white-space: nowrap;
            }

            ._main-item-val {
              //width: 65%;
              display: flex;
              flex-wrap: nowrap;
              justify-content: flex-end;
              align-items: center;
              line-height: 84rpx;
              height: 84rpx;

              ._main-item-val-head {
                width: 48rpx;
                height: 48rpx;
                border-radius: 50%;
                display: flex;

                .head-img {
                  width: 100%;
                  height: 100%;
                  border-radius: 50%;
                }
              }

              ._main-item-val-input {
                text-align: right;
                display: flex;
                height: 100%;

                ._main-item-val-input-text {
                  display: flex;
                  flex-wrap: nowrap;
                  align-items: center;
                }

                .drop-dowm-img {
                  width: 30rpx;
                  height: 30rpx;
                  margin-left: 10rpx;
                  transform: rotateZ(270deg);
                }
              }

              .drop-dowm-img {
                width: 30rpx;
                height: 30rpx;
                margin-left: 10rpx;
                transform: rotateZ(270deg);
              }
            }
          }

          ._content-box-item-chunk {
            width: 100%;
            border-bottom: 1px solid rgba(200, 199, 204, 0.2);
            padding-bottom: 33rpx;

            ._item-chunk-label {
              font-size: 28rpx;
              color: #000;
              font-weight: 600;
              margin-left: 20rpx;
              line-height: 60rpx;
            }

            ._item-chunk-val {
              height: 80rpx;
              background-color: #f6f6f6;
              border-radius: 10rpx;
              font-size: 26rpx;
              color: #000;
              padding: 0 20rpx;
              box-sizing: border-box;
              line-height: 80rpx;
              font-size: 26rpx;
              display: flex;
              flex-wrap: nowrap;
              justify-content: space-between;
              align-items: center;

              ._item-chunk-val-input {
                width: 100%;
                height: 100%;
                line-height: 80rpx;
                font-size: 26rpx;
              }

              ._main-item-val-input {
                text-align: right;
                display: flex;
                width: 100%;
                height: 100%;

                /deep/ div {
                  // width: 100%;
                  // display: flex;
                  // flex-wrap: nowrap;
                  // justify-content: space-between;
                  // align-items: center;
                }

                ._main-item-val-input-text {
                  display: flex;
                  flex-wrap: nowrap;
                  align-items: center;
                }

                .drop-dowm-img {
                  width: 30rpx;
                  height: 30rpx;
                  margin-left: 10rpx;
                  transform: rotateZ(270deg);
                }
              }

              .drop-dowm-img {
                width: 30rpx;
                height: 30rpx;
                margin-left: 10rpx;
                transform: rotateZ(270deg);
              }

              ._val-box {
                width: 100%;
                height: 100%;
                display: flex;
                flex-wrap: nowrap;
                justify-content: space-between;

                ._val-box-input {
                  height: 100%;
                  line-height: 80rpx;
                  font-size: 26rpx;
                }
              }
            }
          }
        }
      }

      ._main-content-box-l {
        margin-left: 0;
      }

      ._main-content-box-r {
        //margin-left: -100%;
      }
    }

    ._main-content-tips {
      width: 100%;
      margin-top: 40rpx;

      ._main-content-tips-text {
        width: 100%;
        text-align: center;
        font-size: 24rpx;
        color: #888;
      }
    }

    ._main-content-confirm {
      width: 100%;
      height: 80rpx;
      background-color: #037aff;
      border-radius: 10rpx;
      line-height: 80rpx;
      text-align: center;
      font-size: 32rpx;
      color: #fff;
      margin-top: 40rpx;
    }
  }
}
._main-item {
  // height: 86rpx;
  border: 1px solid rgba(200, 199, 204, 0.5);
  padding: 5rpx 20rpx;
  border-radius: 10rpx;
  margin-bottom: 40rpx;
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
  align-items: center;

  ._main-item-chunk {
    font-size: 34rpx;
    color: #000;
    line-height: 84rpx;
  }

  ._main-item-label {
    //width: 35%;
    //min-width: 210rpx;
    white-space: nowrap;
  }

  ._main-item-val {
    //width: 65%;
    display: flex;
    flex-wrap: nowrap;
    justify-content: flex-end;
    align-items: center;
    line-height: 84rpx;
    height: 84rpx;

    ._main-item-val-head {
      width: 48rpx;
      height: 48rpx;
      border-radius: 50%;
      display: flex;

      .head-img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }

    ._main-item-val-input {
      text-align: right;
      display: flex;
      height: 100%;

      ._main-item-val-input-text {
        display: flex;
        flex-wrap: nowrap;
        align-items: center;
      }

      .drop-dowm-img {
        width: 30rpx;
        height: 30rpx;
        margin-left: 10rpx;
        transform: rotateZ(270deg);
      }
    }

    .drop-dowm-img {
      width: 30rpx;
      height: 30rpx;
      margin-left: 10rpx;
      transform: rotateZ(270deg);
    }
  }
}

._content-box-item-chunk {
  width: 100%;
  border-bottom: 1px solid rgba(200, 199, 204, 0.2);
  padding-bottom: 33rpx;

  ._item-chunk-label {
    font-size: 28rpx;
    color: #000;
    font-weight: 600;
    margin-left: 20rpx;
    line-height: 60rpx;
  }

  ._item-chunk-val {
    height: 80rpx;
    background-color: #f6f6f6;
    border-radius: 10rpx;
    font-size: 26rpx;
    color: #000;
    padding: 0 20rpx;
    box-sizing: border-box;
    line-height: 80rpx;
    font-size: 26rpx;
    display: flex;
    flex-wrap: nowrap;
    justify-content: space-between;
    align-items: center;

    ._item-chunk-val-input {
      width: 100%;
      height: 100%;
      line-height: 80rpx;
      font-size: 26rpx;
    }

    ._main-item-val-input {
      text-align: right;
      display: flex;
      width: 100%;
      height: 100%;

      /deep/ div {
        // width: 100%;
        // display: flex;
        // flex-wrap: nowrap;
        // justify-content: space-between;
        // align-items: center;
      }

      ._main-item-val-input-text {
        display: flex;
        flex-wrap: nowrap;
        align-items: center;
      }

      .drop-dowm-img {
        width: 30rpx;
        height: 30rpx;
        margin-left: 10rpx;
        transform: rotateZ(270deg);
      }
    }

    .drop-dowm-img {
      width: 30rpx;
      height: 30rpx;
      margin-left: 10rpx;
      transform: rotateZ(270deg);
    }

    ._val-box {
      width: 100%;
      height: 100%;
      display: flex;
      flex-wrap: nowrap;
      justify-content: space-between;

      ._val-box-input {
        height: 100%;
        line-height: 80rpx;
        font-size: 26rpx;
      }
    }
  }
}
</style>
